package org.snowin.utils;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;

public class Htmlunit
{



    public static WebClient webClient = null;
    private static final Htmlunit htmlUnit = new Htmlunit();

    public static Htmlunit getInstance()
    {
        if (null == htmlUnit) {
            return new Htmlunit();
        }
        return htmlUnit;
    }

    private Htmlunit()
    {
        String proxyHost = System.getProperty("proxyHost");
        if (proxyHost.length() > 0)
        {
            int port = Integer.parseInt(System.getProperty("proxyPort"));
            webClient = new WebClient(BrowserVersion.CHROME, proxyHost, port);
        }
        else
        {
            webClient = new WebClient(BrowserVersion.CHROME);
        }
        System.setProperty("apache.commons.httpclient.cookiespec", "compatibility");
        webClient.getOptions().setRedirectEnabled(false);
        webClient.getCookieManager().setCookiesEnabled(false);
        webClient.getOptions().setJavaScriptEnabled(true);
        webClient.getOptions().setCssEnabled(false);
        webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
        webClient.getOptions().setThrowExceptionOnScriptError(false);
        webClient.getOptions().setPrintContentOnFailingStatusCode(false);
        webClient.setAjaxController(new NicelyResynchronizingAjaxController());
        webClient.getOptions().setTimeout(10000);
        webClient.waitForBackgroundJavaScript(6000L);
        webClient.setJavaScriptTimeout(3000L);
        webClient.getOptions().setUseInsecureSSL(false);
        Logger.getLogger("com.gargoylesoftware.htmlunit").setLevel(Level.OFF);
        Logger.getLogger("org.apache.http").setLevel(Level.OFF);
    }

    public String doGet(String url)
            throws Exception
    {
        HtmlPage page = null;
        try
        {
            page = (HtmlPage)webClient.getPage(url);
            resetWebClientTimeout();
        }
        catch (Exception ell)
        {
            increaseTimeout();
            return doGet(url);
        }
        String pageXml = page.asXml();
        webClient.getCache().clear();
        return pageXml;
    }

    public WebClient getWebClient()
    {
        return webClient;
    }

    public void increaseTimeout()
    {
        webClient.waitForBackgroundJavaScript(webClient.getJavaScriptTimeout() + 100L);
        webClient.getOptions().setTimeout(webClient.getOptions().getTimeout() + 1500);
        webClient.setJavaScriptTimeout(webClient.getJavaScriptTimeout() + 100L);
    }

    public void resetWebClientTimeout()
    {
        webClient.waitForBackgroundJavaScript(500L);
        webClient.getOptions().setTimeout(1500);
        webClient.setJavaScriptTimeout(500L);
    }
}
